import React, {useEffect, useState} from 'react';
import {Route, Routes} from "react-router-dom";
import Home from "../../view/sandbox/home/Home";
import UserList from "../../view/sandbox/user-manage/UserList";
import RoleList from "../../view/sandbox/right-manage/RoleList";
import RightList from "../../view/sandbox/right-manage/RightList";
import NewsAdd from "../../view/sandbox/news-manage/NewsAdd";
import NewsDraft from "../../view/sandbox/news-manage/NewsDraft";
import NewsCategory from "../../view/sandbox/news-manage/NewsCategory";
import Audit from "../../view/sandbox/audit-manage/Audit";
import AuditList from "../../view/sandbox/audit-manage/AuditList";
import Unpublished from "../../view/sandbox/publish-manage/Unpublished";
import Published from "../../view/sandbox/publish-manage/Published";
import Sunset from "../../view/sandbox/publish-manage/Sunset";
import axios from "axios";
import Nopermission from "../../view/sandbox/nopermission/Nopermission";
import NewsPreview from "../../view/sandbox/news-manage/NewsPreview";
import NewsUpdate from "../../view/sandbox/news-manage/NewsUpdate";
import {Spin} from "antd";
import {connect} from "react-redux";


const RouteListMap={
    "/home":<Home/>,
    "/user-manage/list":<UserList/>,
    "/right-manage/role/list":<RoleList/>,
    "/right-manage/right/list":<RightList/>,
    "/news-manage/add":<NewsAdd/>,
    "/news-manage/draft":<NewsDraft/>,
    "/news-manage/category":<NewsCategory/>,
    "/news-manage/preview/:id":<NewsPreview/>,
    "/news-manage/update/:id":<NewsUpdate/>,
    "/audit-manage/audit":<Audit/>,
    "/audit-manage/list":<AuditList/>,
    "/publish-manage/unpublished":<Unpublished/>,
    "/publish-manage/published":<Published/>,
    "/publish-manage/sunset":<Sunset/>
}
const NewsRouter = (props) => {
    const [RouteList,setRouteList]=useState([])

    const {role:{rights}}=JSON.parse(localStorage.getItem('token'))

    useEffect(()=>{
        Promise.all([
            axios.get('/rights'),
            axios.get('/children'),
        ]).then(res=>{
            setRouteList([...res[0].data,...res[1].data])
        })
    },[])

    const checkRoute=(item)=>{
        return RouteListMap[item.key] && (item.pagepermisson || item.routepermisson)
    }

    const checkUserPermission=(item)=>{
        return rights.includes(item.key)
    }
    return (
        <Spin size="large" spinning={props.isLoading}>
            <Routes>
                {
                    RouteList.map(item=>{
                        if(checkRoute(item)&&checkUserPermission(item)){
                            return <Route path={item.key} key={item.key} element={RouteListMap[item.key]} exact/>
                        }
                        return <Route path={item.key} key={item.key} element={<Nopermission/>}/>
                    })
                }
            </Routes>
        </Spin>

    );

}
const mapStateToProps=({LoadingReducer:{isLoading}})=>{
    return {
        isLoading
    }
}

export default connect(mapStateToProps)(NewsRouter);